// pages/editpurchas/editpurchas.js
var app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    XfabricList: app.globalData.XfabricList,
    XauxiliaryFabricList: app.globalData.XauxiliaryFabricList,
    total:0,
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      id: options.id,
      type: options.type
    })
    var that = this,
      type = options.type,
      index = options.index;
    if (type =='退回'){
      wx.request({
        url: app.globalData.api + 'purchasingOrderForm/' + options.id,
        data: {},
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data.data)
          if (res.data.code == 200) {
            var fabricList = [], auxiliaryFabricList = []
            for (var i in res.data.data.fabricList) {
              if (res.data.data.fabricList[i].confirm == false) {
                res.data.data.fabricList[i].width = res.data.data.fabricList[i].fabricArrearsAmount + res.data.data.fabricList[i].fabricCallBackAmount;
                fabricList.push(res.data.data.fabricList[i])
              }
            }
            for (var i in res.data.data.auxiliaryFabricList) {
              if (res.data.data.auxiliaryFabricList[i].confirm == false) {
                res.data.data.auxiliaryFabricList[i].width = res.data.data.auxiliaryFabricList[i].auxiliaryFabricCallBackAmount + res.data.data.auxiliaryFabricList[i].auxiliaryFabricArrearsAmount;
                auxiliaryFabricList.push(res.data.data.auxiliaryFabricList[i])
              }
            }
            app.globalData.XfabricList = fabricList;
            app.globalData.XauxiliaryFabricList = auxiliaryFabricList;
            that.setData({
              XfabricList: fabricList,
              XauxiliaryFabricList: auxiliaryFabricList,
            })
          }
        }
      })
    }else{
      wx.request({
        url: app.globalData.api + 'purchasingOrderForm/' + options.id,
        data: {},
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data.data)
          if (res.data.code == 200) {
            var fabricListPrice = 0, auxiliaryFabricListPrice = 0
            for (var i in res.data.data.fabricList) {
              fabricListPrice += parseInt(res.data.data.fabricList[i].fabricPrice)
            }
            for (var i in res.data.data.auxiliaryFabricList) {
              auxiliaryFabricListPrice += parseInt(res.data.data.auxiliaryFabricList[i].auxiliaryFabricPrice)
            }
            app.globalData.XfabricList = res.data.data.fabricList;
            app.globalData.XauxiliaryFabricList = res.data.data.auxiliaryFabricList;
            that.setData({
              XfabricList: app.globalData.XfabricList,
              XauxiliaryFabricList: app.globalData.XauxiliaryFabricList,
              total: fabricListPrice + auxiliaryFabricListPrice
            })
          }
        }
      })
    }
   
  },
  edilt: function (e) {
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&index=' + e.currentTarget.dataset.index+'&i=true&isTui='+this.data.type
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var fabricListPrice = 0, auxiliaryFabricListPrice = 0
    for (var i in app.globalData.XfabricList) {
      fabricListPrice += Number(app.globalData.XfabricList[i].fabricPrice)
    }
    for (var i in app.globalData.XauxiliaryFabricList) {
      auxiliaryFabricListPrice += Number(app.globalData.XauxiliaryFabricList[i].auxiliaryFabricPrice)
    }
    this.setData({
      XfabricList: app.globalData.XfabricList,
      XauxiliaryFabricList: app.globalData.XauxiliaryFabricList,
      total: (fabricListPrice + auxiliaryFabricListPrice).toFixed(2)
    })
  },
  sub:function(){
    var that = this
    if(this.data.type == '退回'){
      wx.request({
        url: app.globalData.api + 'purchasingOrderForm/reissueFabric/' + this.data.id,
        data: {
          fabricList: app.globalData.XfabricList,
          auxiliaryFabricList: app.globalData.XauxiliaryFabricList
        },
        header: {
          Authorization: wx.getStorageSync('token')
        },
        method: 'PUT',
        success: function (res) {
          if (res.data.code == 200) {
            app.globalData.XfabricList = []
            app.globalData.XauxiliaryFabricList = []
            wx.showToast({
              title: res.data.msg,
            })
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showToast({
              icon:'none',
              title: res.data.errorMsg,
            })
          }
        }
      })
    }else{
      wx.request({
        url: app.globalData.api + 'purchasingOrderForm/' + this.data.id,
        data: {
          fabricList: app.globalData.XfabricList,
          auxiliaryFabricList: app.globalData.XauxiliaryFabricList
        },
        header: {
          Authorization: wx.getStorageSync('token')
        },
        method: 'PUT',
        success: function (res) {
          if (res.data.code == 200) {
            app.globalData.XfabricList = []
            app.globalData.XauxiliaryFabricList = []
            wx.showToast({
              title: res.data.msg,
            })
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.errorMsg,
            })
          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})